﻿@inherits LayoutComponentBase
@inject NavigationManager Navigation

<SLayout OnErrorAsync="OnError" IsShowEnvironmentSwitch="true"
         Logo="https://cdn.masastack.com/stack/images/logo/MASAStack/logo-h-en.png"
         MiniLogo="https://cdn.masastack.com/stack/images/logo/MASAStack/logo.png">
    @Body
</SLayout>

@code {
    [Inject]
    public IPopupService PopupService { get; set; } = default!;

    [Inject]
    public I18n I18N { get; set; } = default!;

    Task OnError(Exception exception)
    {
        PopupService.EnqueueSnackbarAsync(exception.Message, AlertTypes.Error);
        return Task.CompletedTask;
    }

    protected bool TranslateFluentValidationProperty { get; set; } = true;

    private void UsingI18nToResolveFluentValidationDisplayName(I18n i18N)
    {
        ValidatorOptions.Global.DisplayNameResolver = (type, member, expression) =>
        {
            if (member != null)
            {
                var memberName = i18N.T(member.Name);
                return memberName;
            }
            return null;
        };
    }

    protected override Task OnAfterRenderAsync(bool firstRender)
    {
        if (TranslateFluentValidationProperty)
        {
            UsingI18nToResolveFluentValidationDisplayName(I18N);
        }
        return base.OnAfterRenderAsync(firstRender);
    }
}